package com.example.demo.Controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.GeneralResponse.APIResponse;
import com.example.demo.Service.EmployeeService;
import com.example.demo.Service.HRService;
import com.example.demo.Service.TokenService;
import com.example.demo.Service.UserService;
import com.example.demo.utils.DateFormatter;

/**
 * @version: V0.3
 * @author: 郑皓杰 吴嘉鑫
 * @className: ModifyController
 * @packageName: com.example.demo.Controller
 * @description: 这是用户进行信息修改的控制类
 * @data: 2021-07-14
 **/


@RestController
@RequestMapping("/Modify")
public class ModifyController {

	@Autowired
	private EmployeeService empService;
	@Autowired
	private HRService hrService;
	@Autowired
	private TokenService tokenService;
	@Autowired
	private UserService userService;
	
	
	/**
	* @author:郑皓杰
	* @methodsName: setInfo
	* @description: employee更改自己的个人信息
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/ModifyEmp")
	public APIResponse<?> setInfo(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		String token = httpServletRequest.getHeader("Token");
		Integer Empid = tokenService.getEmpidfromToken(token);
	    String realname=httpServletRequest.getParameter("realname");
	    String gender=httpServletRequest.getParameter("gender");
	    Date birth=DateFormatter.DateTransform(httpServletRequest.getParameter("birth"));
	    String email=httpServletRequest.getParameter("email");
	    String telephone=httpServletRequest.getParameter("telephone");
	    Integer expectedsalary=Integer.parseInt(httpServletRequest.getParameter("expectedsalary"));
	    return empService.setInfo(Empid,realname,gender,birth,email,telephone,expectedsalary);
	}
	
	
	/**
	* @author:吴嘉鑫
	* @methodsName: confirm
	* @description: 录用一个员工的过程：首先是处于自由人状态
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/confirm")
	public APIResponse<?> confirm(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		String tel = httpServletRequest.getParameter("telephone");
		return hrService.confirm(tel);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: search
	* @description: 录用一个员工的过程：然后返回需要用户验证查看的信息
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/searchEmp")
	public APIResponse<?> search(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		String tel = httpServletRequest.getParameter("telephone");
		return hrService.search(tel);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: recruitEmp
	* @description: 录用一个员工的过程：最后修改数据库中员工的所属公司，并生成相关经历
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/recruitEmp")
	public APIResponse<?> recruitEmp(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		String tel = httpServletRequest.getParameter("telephone");
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token);
		String companyName = tokenService.getCompanyNamefromToken(token);
		String position = httpServletRequest.getParameter("position");
		Integer salary = Integer.parseInt(httpServletRequest.getParameter("salary"));
		return hrService.recruit(tel,companyID,companyName,position,salary);
	}
	
	
	/**
	* @author:吴嘉鑫
	* @methodsName: setPosition
	* @description: 修改职位
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/setPosition")
	public APIResponse<?> setPosition(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		//Integer companyID = ((HumanResource)httpServletRequest.getSession().getAttribute("hr")).getCompanyID();
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token);
		String tel = httpServletRequest.getParameter("tel");
		String position = httpServletRequest.getParameter("position");
		return hrService.setPosition(tel,companyID,position);
	}
	
	
	/**
	* @author:吴嘉鑫
	* @methodsName: setSalary
	* @description: 修改薪资
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/setSalary")
	public APIResponse<?> setSalary(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		//Integer companyID = ((HumanResource)httpServletRequest.getSession().getAttribute("hr")).getCompanyID();
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token);
		String tel = httpServletRequest.getParameter("tel");
		Integer salary = Integer.parseInt(httpServletRequest.getParameter("salary"));
		return hrService.setSalary(tel,companyID,salary);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: suspend
	* @description: 离职员工
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/suspend")
	public APIResponse<?> suspend(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		//Integer companyID = ((HumanResource)httpServletRequest.getSession().getAttribute("hr")).getCompanyID();
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token);
		String tel = httpServletRequest.getParameter("tel");
		String evaluation=httpServletRequest.getParameter("evaluation");
		return hrService.suspend(tel,companyID,evaluation);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: evaluate
	* @description: 修改评价
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	/*@CrossOrigin
	@RequestMapping("/evaluate")
	public APIResponse<?> evaluate(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		//Integer companyID = ((HumanResource)httpServletRequest.getSession().getAttribute("hr")).getCompanyID();
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token);
		String tel = httpServletRequest.getParameter("tel");
		String evaluation = httpServletRequest.getParameter("evaluation");
		return hrService.evaluate(tel,companyID,evaluation);
	}
	*/
	/**
	* @author:吴嘉鑫
	* @methodsName: deleteMes
	* @description: 删除讯息
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/deleteMes")
	public APIResponse<?> deleteMes(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		Integer mesid = Integer.parseInt(httpServletRequest.getParameter("mesid"));
		return userService.deleteMyMessage(mesid);
	}
}
